// mircoApp/numberCollection/collectionList/collectionList.js
const app = getApp();
const skinBehavior = require("../../../utils/themeSkin/skinBehavior");
Page({
  behaviors: [skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    sponShow:false,
    changeShow:false,
    maxtotal:'',
    params:{
      transfer_id:'',
      goods_id:'',
      total:1
    },
    exParams:{
      total:1,
      goods_id:"",
      address:{
        province:"",
        city:"",
        district:"",
        mobile:"",
        username:'',
        street:'',
        address:''
      }
    },
    page:1,
    isLoadMore: true,
    totalPage: 0,
    timeout:'',
    myList:[],
    maxtotal:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },
  sponsorClick(event) {
    let {goodsid,usetotal} = event.currentTarget.dataset;
    this.setData({
      sponShow:true,
      'params.goods_id':goodsid,
      maxtotal:Number(usetotal)
    })
  },
  covertClick(event) {
    let {goodsid,usetotal} = event.currentTarget.dataset;
    this.setData({
      changeShow:true,
      'exParams.goods_id':goodsid,
      maxtotal:Number(usetotal)
    })
  },
  exchangeClick(event) {
    if(app._isTextEmpty(this.data.maxtotal)) {
      wx.showToast({
        icon: "none",
        title: "没有可转赠的藏品",
        duration: 1000,
      });
      return false;
    }
    if(app._isTextEmpty(this.data.exParams.total)) {
      wx.showToast({
        icon: "none",
        title: "商品数量错误",
        duration: 1000,
      });
      return false;
    }
    if(app._isTextEmpty(this.data.exParams.address.username)) {
      wx.showToast({
        icon: "none",
        title: "请输入收件人",
        duration: 1000,
      });
      return false;
    }
    if(app._isTextEmpty(this.data.exParams.address.mobile)) {
      wx.showToast({
        icon: "none",
        title: "请输入手机号",
        duration: 1000,
      });
      return false;
    }
    if(app._isTextEmpty(this.data.exParams.address.province) || app._isTextEmpty(this.data.exParams.address.city) || app._isTextEmpty(this.data.exParams.address.district)) {
      wx.showToast({
        icon: "none",
        title: "请输入地址",
        duration: 1000,
      });
      return false;
    }
    if(app._isTextEmpty(this.data.exParams.address.address)) {
      wx.showToast({
        icon: "none",
        title: "请输入详细地址",
        duration: 1000,
      });
      return false;
    }
    if (this.data.timeout) {
      clearTimeout(this.data.timeout);
    }
    wx.showLoading({
      title: '加载中...',
      mask:true
    });
    this.data.timeout = setTimeout(() => {
      this.exchangeFn();
     
    }, 300);
  },
  exchangeFn() {
    const exchangePop = this.selectComponent('#exchangePop');
    let urlStr = app.getNetAddresss("plugin.collection-center.frontend.controllers.collection.collection-manage.exchange");
    app._postNetWork({
      url: urlStr,
      data: this.data.exParams,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          exchangePop.setData({
            changeShow:false,
            strShow:false
          })
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 2500,
          });
          wx.hideLoading();
          this.initData();
          this.collectionList();
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  sponsorConfrim(event) {
    let detail = event.detail;
    this.setData({
      'params.transfer_id':Number(detail.memberId),
      'params.total':detail.maxSum
    })
    if(app._isTextEmpty(this.data.maxtotal)) {
      wx.showToast({
        icon: "none",
        title: "没有可转赠的藏品",
        duration: 1000,
      });
      return false;
    }
    if(app._isTextEmpty(this.data.params.goods_id)) {
      wx.showToast({
        icon: "none",
        title: "订单ID错误",
        duration: 1000,
      });
      return false;
    }
    // this.params.transfer_id = Number(this.data.params.transfer_id);
    if(app._isTextEmpty(this.data.params.transfer_id)) {
      wx.showToast({
        icon: "none",
        title: "会员ID错误",
        duration: 1000,
      });
      return false;
    }
    if(app._isTextEmpty(this.data.params.total)) {
      wx.showToast({
        icon: "none",
        title: "商品数量错误",
        duration: 1000,
      });
      return false;
    }
    let sponPopup = this.selectComponent('#sponPopup');
    let urlStr = app.getNetAddresss("plugin.collection-center.frontend.controllers.collection.collection-manage.transfer");
    app._postNetWork({
      url: urlStr,
      data: this.data.params,
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1000,
          });
          sponPopup.setData({
            memberFlag:false,
            sponShow:false
          })
          this.initData();
          this.collectionList();
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  areaConfirm(event) {
    let detail = event.detail;
    this.setData({
      'exParams.address.province':detail[0].name,
      'exParams.address.city':detail[1].name,
      'exParams.address.district':detail[2].name,
      addressName:`${detail[0].name}${detail[1].name}${detail[2].name}`,
    })
  },
  inputChnage(event) {
    let {type,value} = event.detail;
    this.setData({
      [`exParams.${type}`]:value
    })
  },
  examount(event) {
    this.setData({
      'exParams.total':event.detail?.value
    })
  },
  streetConfirm(event) {
    let detail = event.detail;
    this.setData({
      'exParams.address.street':detail.areaname,
    });
  },
  initData() {
    let params = {
      transfer_id:'',
      order_id:'',
      total:1
    };
    let exParams = {
      total:1,
      order_id:"",
      address:{
        province:"",
        city:"",
        district:"",
        mobile:"",
        username:'',
        street:'',
        address:''
      }
    };
    this.setData({
      exParams,
      params,
      page:1,
      addressName:''
    })
  },
  collectionList() {
    let query = {
      page:this.data.page,
    };
    let urlStr = app.getNetAddresss("plugin.collection-center.frontend.controllers.collection.collection-manage");
    app._postNetWork({
      url: urlStr,
      data:query,
      success:  (resdata) => {
        let res = resdata.data;
        let data = res.data;
        wx.hideLoading();
        wx.hideNavigationBarLoading();//隐藏导航条加载动画
        //停止下拉刷新
        wx.stopPullDownRefresh();
        if (res.result == 1) {
          this.data.totalPage = data.collection_list?.last_page;
          if (!this.data.totalPage) {
            this.data.totalPage = 0;
          }
          this.setData({
            myList: data.collection_list.data
          })
        } else {
          this.data.page = this.data.page - 1;
          this.data.isLoadMore = false;
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  //获取更多数据
  getMoreData() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.totalPage) {
      return;
    } else {
      this.data.page = this.data.page + 1;
      let query = {
        page:this.data.page,
      };
      let urlStr = app.getNetAddresss("plugin.collection-center.frontend.controllers.collection.collection-manage");
      app._postNetWork({
        url: urlStr,
        data: query,
        success: (resdata) => {
          let res = resdata.data;
          let list = res.data.collection_list?.data;
          if (res.result == 1) {
            this.data.isLoadMore = true;
            this.setData({
              myList : this.data.myList.concat(list),
            })
          } else {
            this.data.page = this.data.page - 1;
            this.isLoadMore = false;
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.data.page = 1;
    this.data.myList = [];
    this.collectionList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    wx.showNavigationBarLoading();
     //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
     wx.showLoading({
      title: '刷新中...',
    })
    this.setData({
      page:1,
      myList:[],
      isLoadMore: true,
    })
    this.collectionList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多了');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})